package com.movie.controller;

import com.movie.dao.MovieDao;
import com.movie.model.Movie;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MovieController extends HttpServlet {
    private MovieDao movieDao;

    @Override
    public void init() throws ServletException {
        movieDao = new MovieDao();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action == null) {
            action = "list"; // 默认值
        }
        if ("list".equals(action)) {
            // 获取查询条件
            String name = request.getParameter("name");
            String director = request.getParameter("director");
            String genre = request.getParameter("genre");

            // 使用 DAO 层过滤电影
            List<Movie> movieList = movieDao.getMovies(name, director, genre);

            // 将查询条件和结果传递给 JSP 页面
            request.setAttribute("movieList", movieList);
            request.setAttribute("name", name);
            request.setAttribute("director", director);
            request.setAttribute("genre", genre);

            // 跳转到显示电影列表的页面
            request.getRequestDispatcher("movie-list.jsp").forward(request, response);
        } else if ("delete".equals(action)) {
            // 删除电影
            int movieId = Integer.parseInt(request.getParameter("id"));
            movieDao.deleteMovie(movieId);
            response.sendRedirect("movie?action=list");
        } else if ("edit".equals(action)) {
            // 编辑电影
            int movieId = Integer.parseInt(request.getParameter("id"));
            Movie movie = movieDao.getMovieById(movieId);
            request.setAttribute("movie", movie);
            request.getRequestDispatcher("movie-edit.jsp").forward(request, response);
        } else if ("add".equals(action)) {
            // 跳转到添加电影页面
            request.getRequestDispatcher("movie-add.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置请求和响应的编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");

        if ("add".equals(action)) {
            String name = request.getParameter("name");
            String director = request.getParameter("director");
            String genre = request.getParameter("genre");
            String releaseDateStr = request.getParameter("releaseDate");

            // 解析日期字符串
            java.sql.Date releaseDate = null;
            if (releaseDateStr != null && !releaseDateStr.isEmpty()) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date parsedDate = sdf.parse(releaseDateStr);
                    releaseDate = new java.sql.Date(parsedDate.getTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            // 创建电影对象并添加到数据库
            Movie movie = new Movie(name, director, genre, releaseDate);
            movieDao.addMovie(movie);
            response.sendRedirect("movie?action=list");
        } else if ("update".equals(action)) {
            int movieId = Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("name");
            String director = request.getParameter("director");
            String genre = request.getParameter("genre");
            String releaseDateStr = request.getParameter("releaseDate");

            // 解析日期字符串
            java.sql.Date releaseDate = null;
            if (releaseDateStr != null && !releaseDateStr.isEmpty()) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date parsedDate = sdf.parse(releaseDateStr);
                    releaseDate = new java.sql.Date(parsedDate.getTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            // 创建电影对象并更新到数据库
            Movie movie = new Movie(movieId, name, director, genre, releaseDate);
            movieDao.updateMovie(movie);
            response.sendRedirect("movie?action=list");
        }
    }
}
